package com.capinfo.accounting.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;


import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.HZJZPZServiceI;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;
import com.google.gson.Gson;


@Controller
@RequestMapping("/business/accounting/pingzhengfuhe")
public class PzfhController {
	@Autowired
	public GB_JZPZXXServiceI jzpxxService;
	@Autowired
	HZJZPZServiceI hZJZPZService;
	//查询未复核的数据
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return jzpxxService.findbyByGrid(param);
	} 
	//复合
	@RequestMapping("/update.shtml")
	@ResponseBody
	public Json update(String ids, HttpSession session,NativeWebRequest request){
		GB_JZPZXX jzpzxx =new GB_JZPZXX();
		Json j = new Json();
		long id=0;
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		String fh=user.getLogonName();
		Date date=new Date();
		String[] idsArray=ids.split(",");

		try {
			
			for(int i=0;i<idsArray.length;i++){
				 id=Long.parseLong(idsArray[i]);
				 	jzpzxx.setID(id);
					jzpzxx.setFH(fh);
					jzpzxx.setFHRQ(date);
					jzpxxService.updateFhByID(jzpzxx);
			}
			
			j.setSuccess(true);
			j.setMsg("复核成功");
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
			
		}
	
		return j;
	}
	//复合
		@RequestMapping("/removeUpdate.shtml")
		@ResponseBody
		public Json removeUpdate(String ids, HttpSession session,NativeWebRequest request){
			GB_JZPZXX jzpzxx =new GB_JZPZXX();
			Json j = new Json();
			long id=0;
			String fh="";
			Date date=null;
			String[] idsArray=ids.split(",");

			try {
				
				for(int i=0;i<idsArray.length;i++){
					 id=Long.parseLong(idsArray[i]);
					 	jzpzxx.setID(id);
						jzpzxx.setFH(fh);;
						jzpzxx.setFHRQ(date);
						jzpxxService.updateFhByID(jzpzxx);
				}
				
				j.setSuccess(true);
				j.setMsg("已取消复核");
			} catch (Exception e) {
				e.printStackTrace();
				j.setMsg(e.getMessage());
				
			}
		
			return j;
		}
		@RequestMapping("/addPage.shtml")
		public String addPage(String ids,HttpServletRequest request) {  
			Map map = new HashMap();
			map.put("ids", ids);
			request.setAttribute("ids", map);
			return "/business/accounting/pingzhengfuhe/addQuestion";
		}
		//添加问题标注
		@RequestMapping("/addQuestion.shtml")
		@ResponseBody
		public Json addQuestion(String ids,String wtsm, HttpSession session,NativeWebRequest request){

			GB_JZPZXX jzpzxx =new GB_JZPZXX();
			Json j = new Json();
			long id=0;
			String[] idsArray=ids.split(",");

			try {
				
				for(int i=0;i<idsArray.length;i++){
					 id=Long.parseLong(idsArray[i]);
					 	jzpzxx.setID(id);
						jzpzxx.setWTSM(wtsm);
						jzpxxService.updateWtsmById(jzpzxx);
				}
				
				j.setSuccess(true);
				j.setMsg("已添加问题说明");
			} catch (Exception e) {
				e.printStackTrace();
				j.setMsg(e.getMessage());
				
			}
		
			return j;
		}
		//清空问题标注
				@RequestMapping("/removeQuestion.shtml")
				@ResponseBody
				public Json removeQuestion(String ids,HttpSession session,NativeWebRequest request){

					GB_JZPZXX jzpzxx =new GB_JZPZXX();
					Json j = new Json();
					long id=0;
					String wtsm="";
					String[] idsArray=ids.split(",");

					try {
						
						for(int i=0;i<idsArray.length;i++){
							 id=Long.parseLong(idsArray[i]);
							 	jzpzxx.setID(id);
								jzpzxx.setWTSM(wtsm);
								jzpxxService.updateWtsmById(jzpzxx);
						}
						
						j.setSuccess(true);
						j.setMsg("已移除问题说明");
					} catch (Exception e) {
						e.printStackTrace();
						j.setMsg(e.getMessage());
						
					}
				
					return j;
				}
				@RequestMapping(value="/hsdw")  
				@ResponseBody  
				public void hsdw (HttpServletRequest request,HttpServletResponse response) throws IOException  {
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					 Gson gson = new Gson();
					   Map map = new HashMap();
					   List<Map<String,Object>>hsdw= hZJZPZService.hsdw();
					   PrintWriter pw;
					   pw = response.getWriter();
					   pw.write(gson.toJson(hsdw));
				   }
				

				
				//查帐套
				@RequestMapping(value="/cwzt")  
				@ResponseBody  
				public void cwzt (HttpServletRequest request,HttpServletResponse response) throws IOException  {
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					 Gson gson = new Gson();
					   Map map = new HashMap();
					   List<Map<String,Object>>cwzt= hZJZPZService.cwzt();
					   PrintWriter pw;
					   pw = response.getWriter();
					   pw.write(gson.toJson(cwzt));
				   }
		
		
}
